/************************************************************
  *  * Hyphenate CONFIDENTIAL 
  * __________________ 
  * Copyright (C) 2016 Hyphenate Inc. All rights reserved. 
  *  
  * NOTICE: All information contained herein is, and remains 
  * the property of Hyphenate Inc..
  * Dissemination of this information or reproduction of this material 
  * is strictly forbidden unless prior written permission is obtained
  * from Hyphenate Inc.
  */
package com.dai.zhenfujin.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.dai.zhenfujin.MainActivity;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.utils.UIUtils;
import com.dai.zhenfujin.visitingcard.activity.LaunchActivity;
import com.hyphenate.chat.EMChatService;
import com.hyphenate.push.EMPushHelper;
import com.hyphenate.push.EMPushType;
import com.hyphenate.util.EMLog;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * start service if user enabled start from boot.
 */
public class EMiMsgReceiver2 extends PushMessageReceiver {
	private static final String TAG = "EMMiMsgReceiver2";
	private int i = 0;

	public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
		EMLog.i(TAG, "onReceivePassThroughMessage is called. " + message.toString());
	}

	public void onNotificationMessageClicked(Context context, MiPushMessage message) {
		EMLog.i(TAG, "onNotificationMessageClicked is called. " + message.toString());
		Intent msgIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
		msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(msgIntent);
	}

	public void onNotificationMessageArrived(Context context, MiPushMessage message) {
		EMLog.i(TAG, "onNotificationMessageArrived is called. " + message.toString());
		i++;
		try {
			Class  miuiNotificationClass = Class.forName("android.app.MiuiNotification");
			Object miuiNotification      = miuiNotificationClass.newInstance();
			Field  field                 = miuiNotification.getClass().getDeclaredField("messageCount");
			field.setAccessible(true);
			field.set(miuiNotification, String.valueOf(i == 0 ? "" : i)); // 设置信息数-->这种发送必须是miui6才行
		} catch (Exception e) {
			e.printStackTrace();
			// miui 6之前的版本
			Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
			localIntent.putExtra("android.intent.extra.update_application_component_name", context.getPackageName() + "/." + LaunchActivity.class.getSimpleName());
			localIntent.putExtra("android.intent.extra.update_application_message_text", String.valueOf(i));
			context.sendBroadcast(localIntent);
		}
	}

	public void onCommandResult(Context context, MiPushCommandMessage message) {
		EMLog.i(TAG, "onCommandResult is called. " + message.toString());
	}

	public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
		String command = message.getCommand();
		List<String> arguments = message.getCommandArguments();
		String cmdArg1 = arguments != null && arguments.size() > 0 ? (String) arguments.get(0) : null;
		String cmdArg2 = arguments != null && arguments.size() > 1 ? (String) arguments.get(1) : null;
		EMLog.i(TAG, "onReceiveRegisterResult. cmdArg1: " + cmdArg1 + "; cmdArg2:" + cmdArg2);
		if (MiPushClient.COMMAND_REGISTER.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				EMPushHelper.getInstance().onReceiveToken(EMPushType.MIPUSH, cmdArg1);
			} else {
				EMPushHelper.getInstance().onErrorResponse(EMPushType.MIPUSH, message.getResultCode());
			}
		}
	}

}
